package cn.anosi.utils;

import freemarker.template.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by lys on 2017/9/29.
 */
public class CodeGenerator {
	//项目根目录
	private final static String root = System.getProperty("user.dir");
	//模板目录
	private final static String tempDir = root + "/src/main/resources/templates";

	private static Configuration configuration;

	private  static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	static{
		configuration = new Configuration(Configuration.getVersion());
		//指定模板路径
		try {
			configuration.setDirectoryForTemplateLoading(new File(tempDir));
		} catch (IOException e) {
			e.printStackTrace();
		}
		configuration.setDefaultEncoding("utf-8");

		//全局共享变量
		try {
			configuration.setSharedVariable("auth", "lys");
			configuration.setSharedVariable("date", sdf.format(new Date()));
		} catch (TemplateModelException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws ClassNotFoundException, IOException, TemplateException {
		//=========必须填写的字段===========
		String module = "auth"; //模块名
		String[] tables = new String[] { "person" }; //要生成的表
		//=========必须填写的字段===========
		
		MpGenerator.generate(module, tables);// 生成后台代码
		
		String entity = "Organization";
		String entityPackage = "cn.anosi.model."+ module + "." + entity;
		//genPage(entityPackage, entity, module); //生成页面
	}

	/**
	 * 生成页面
	 * @param entityPackage 实体类的包名
	 * @param entity 实体类类名
	 * @throws ClassNotFoundException
	 */
	public static void genPage(String entityPackage, String entity, String module) throws ClassNotFoundException, IOException, TemplateException {
		String root = System.getProperty("user.dir");
		List<String> list = getFieldList(entityPackage);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("entity", entity);
		if(list.contains("disable")){
			map.put("disable", "disable");
			list.remove("disable");
		}else{
			map.put("disable", "");
		}
		map.put("list", list);

		String outDir = root + "/src/main/webapp/WEB-INF/ftl/" +module + "/"+ entity.substring(0, 1).toLowerCase() + entity.substring(1) + "/";
		File parent = new File(outDir);
		parent.mkdirs();
		String listPage = outDir +"list.html";
		String addPage = outDir + "add.html";
		String editPage = outDir + "edit.html";
		gen("list.html.ftl", listPage, map);
		gen("add.html.ftl", addPage, map);
		gen("edit.html.ftl", editPage, map);
	}

	
	public static boolean gen(String templateName, String fileName, Map map) throws TemplateNotFoundException, MalformedTemplateNameException, IOException, TemplateException {
		Template template = configuration.getTemplate(templateName);
		File file = new File(fileName);
		Writer out = new FileWriter(file);
		template.process(map, out);
		out.flush();
		out.close();
		return file.exists();
	}
	
	/**
	 * 反射获取实体类的属性和类型
	 * @param entityPackage 实体类的包名
	 * @return
	 * @throws ClassNotFoundException
	 */
	public static List<String[]> getList(String entityPackage) throws ClassNotFoundException {
		Class<?> obj = Class.forName(entityPackage);
		Field[] fields = obj.getDeclaredFields();
		ArrayList<String[]> list = new ArrayList<>();
		for(Field field : fields){
			if(field.getName().equals("serialVersionUID") || field.getName().equals("del") ){
				continue;
			}
			String[] str = {field.getType().getSimpleName(), field.getName()};
			list.add(str);
		}

		return list;
	}


	/**
	 * 获取属性集合
	 * @param entityPackage
	 * @return
	 * @throws ClassNotFoundException
	 */
	public static List<String> getFieldList(String entityPackage) throws ClassNotFoundException {
		Class<?> obj = Class.forName(entityPackage);
		Field[] fields = obj.getDeclaredFields();
		ArrayList<String> list = new ArrayList<>();
		for(Field field : fields){
			if(field.getName().equals("serialVersionUID") || field.getName().equals("del") ){
				continue;
			}
			list.add(field.getName());
		}

		return list;
	}
}
